package com.cloud.easy.im.handler.strategy;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.argeement.AgreementFactory;
import com.cloud.easy.im.argeement.client.LoginInfo;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.processor.OnlineProcessor;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 收到Client发送登陆请求策略处理器
 *
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月03日6:55 下午
 */
public class AgreementCLoginHandler extends AgreementStrategy {

    private static final Logger logger = LoggerFactory.getLogger(AgreementCLoginHandler.class);

    public AgreementCLoginHandler(byte myType) {
        super(myType);
    }

    @Override
    public void handler(Channel session, Agreement dataFromClient, ServerEventListener serverEventListener) throws Exception {

        LoginInfo loginInfo = AgreementFactory.parseLoginInfo(dataFromClient.getDataContent());

        if (loginInfo == null || loginInfo.getLoginUserId() == null) {
            logger.warn("收到客户端,但loginInfo或loginInfo.getLoginUserId()是null，登陆无法继续[loginInfo=" + loginInfo
                    + ",loginInfo.getLoginUserId()=" + loginInfo.getLoginUserId() + "]！");
            return;
        }
        //判断是否在线
        if (OnlineProcessor.isOnline(session)) {
            //已经登陆过
            logger.warn("[注意]，此客户端已经登陆，正在请求重新登陆。loginUserId：" + loginInfo.getLoginUserId());
        }

    }
}
